package com.itheima.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import com.itheima.cammon.PageInof;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.dto.SetmealDishesDto;
import com.itheima.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 王同学
 */
@Service
@Slf4j
public class SetmealServiceimpl implements SetmealService {
    @Autowired
    SetmealMapper setmealMapper;

    @Override
    public boolean add(Setmeal stemeal) {
        //增删改一般都是用的布尔类型
        SetmealVO setmealVO = new SetmealVO();
        //数据库有两种id字段 所以这边要写一个雪花id
        String id = IdUtil.getSnowflakeNextIdStr();
        stemeal.setId(id);

        //复制一份相同元素的东西 // TODO: 2022/12/4 这边会把菜品表里面的dishId给覆盖掉(已解决)
        BeanUtils.copyProperties(stemeal, setmealVO);

        //去调用集合的元素 套餐菜品表
        List<SetmealDishesDto> dtoList = setmealVO.getSetmealDishes();

        ArrayList<SetmealDishesDto> newlist = new ArrayList<>();
        //判断一下是否存在
        if (!CollectionUtil.isEmpty(dtoList)) {
            //遍历集合并插入到数据库中
            for (SetmealDishesDto dto : dtoList) {
                if (dto == null) {
                    continue;
                }
                SetmealDishesDto setmealDishesDto = new SetmealDishesDto();
                //传值进去
                //设置名字
                setmealDishesDto.setName(dto.getName());
                String ids = IdUtil.getSnowflakeNextIdStr();
                //添加菜品表id
                setmealDishesDto.setId(ids);
                //把套餐id写道菜品表里面
                setmealDishesDto.setSetmealId(stemeal.getId());
                //把菜品id写到菜品表里面进去
                setmealDishesDto.setDishId(dto.getDishId());
                //把价格写进去
                setmealDishesDto.setPrice(dto.getPrice());
                //把copies写进去
                setmealDishesDto.setCopies(dto.getCopies());
                newlist.add(setmealDishesDto);
            }
        }
        //批量添加 菜品表详情
        int rs = setmealMapper.batchAdd(newlist);
        if (rs != newlist.size()) {
            throw new RuntimeException("添加套餐表失败~！！");
        }
        //添加套餐
        int res = setmealMapper.add(stemeal);
        return res > 0;
    }

    @Override
    public PageInof<Setmeal> page(String name, Integer page, Integer pageSize) {
        //分页查询
        PageInof<Setmeal> pageInof = new PageInof<>();
        //直接上公式
        Integer start = (page - 1) * pageSize;
        List<Setmeal> emp = setmealMapper.page(name, start, pageSize);
        Long pageCountByName = setmealMapper.getPageCountByName(name);
        pageInof.setTotal(pageCountByName);
        pageInof.setRecords(emp);
        pageInof.setSize(pageSize);
        pageInof.setCurrent(start);
        return pageInof;
    }

    @Override
    public boolean deleteById(String ids) {
        //根据id删除套餐和菜品表
        //把接收来的字符串拆分成数组,通过这个逗号区分
        String[] splis = ids.split(",");
        if (splis.length == 0) {
            throw new RuntimeException("入参不能为空");
        }
        List<String> list = Arrays.asList(splis);
        //这边去删除套餐
        boolean delete = setmealMapper.delete(list);
        if (!delete) {
            throw new RuntimeException("删除套餐失败~！！");
        }
        //通过套餐id删除菜品表
        boolean res1 = setmealMapper.delBySetmealdishesIds(list);
        if (!res1) {
            throw new RuntimeException("删除菜品表失败~！！");
        }

        return true;
    }

    //根据套餐id查询 该id下的菜品

    public Setmeal findById(String id) {
        return setmealMapper.findById(id);
    }

    @Override
    public boolean update(Setmeal setmeal) {
        //先调用这个对象
        SetmealVO setmealVO = new SetmealVO();
        //然后把这个对象copy进去
        setmeal.setUpdateTime(LocalDateTime.now());

        BeanUtils.copyProperties(setmeal, setmealVO);

        int res = setmealMapper.update(setmealVO);
        if (res != 1) {
            log.error("套餐更新失败~！");
            //如果这边受影响行数不等于1的话，说明更新失败
            return false;
        }
        // 3．重建套餐和菜品表的关系*/
        //先查询个数 计算总个数
        int number = setmealMapper.countBysetmealId(setmeal.getId());
        if (number > 0) {
            //通过id删除原本菜品的口味
            int SetmealId = setmealMapper.delBysetmealId(setmeal.getId());
            //
            if (SetmealId != number) {
                throw new RuntimeException("菜品表删除失败~!!!!");
            }
        }
        //获取当前口味集合
        List<SetmealDishesDto> flavors = setmealVO.getSetmealDishes();

        ArrayList<SetmealDishesDto> newlis = new ArrayList<>();
        //判断集合是否为空 //这种写法性能低，sql性能下降
        if (!CollectionUtil.isEmpty(flavors)) {
            //遍历集合并插入到数据库中
            for (SetmealDishesDto demo : flavors) {
                //如果为空就跳过
                if (demo == null) {
                    continue;
                }
                SetmealDishesDto setmealDishesDto = new SetmealDishesDto();

                //传值进去
                //份数
                setmealDishesDto.setCopies(demo.getCopies());
                //名字
                setmealDishesDto.setName(demo.getName());
                //价格
                setmealDishesDto.setPrice(demo.getPrice());
                //给他创建一个id
                String snowflakeNextIdStr = IdUtil.getSnowflakeNextIdStr();
                //菜品表id
                setmealDishesDto.setId(snowflakeNextIdStr);
                //菜品id
                setmealDishesDto.setDishId(demo.getDishId());
                //传type
                setmealDishesDto.setType(demo.getType());
                setmealDishesDto.setSetmealId(setmealVO.getId());
                newlis.add(setmealDishesDto);
            }
        }
        int rs = setmealMapper.AddsetmealDishes(newlis);
        if (rs != newlis.size()) {
            throw new RuntimeException("菜品表添加失败~！");
        }
        return true;
    }

    @Override
    public boolean updatestatus(String ids, int stu) {
        int res = setmealMapper.updatestatus(ids, stu);
        if (res <= 0) {
            throw new RuntimeException("上架/下架失败");
        }
        return true;
    }


}